#!/bin/bash

function split() {
  # Usage: split "string" "delimiter"
  IFS=$'\n' read -d "" -ra arr <<<"${1//$2/$'\n'}"
  printf '%s\n' "${arr[@]}"
}

function remove_array_dups() {
  # Usage: remove_array_dups "array"
  declare -A tmp_array

  for i in "$@"; do
    [[ $i ]] && IFS=" " tmp_array["${i:- }"]=1
  done

  printf '%s\n' "${!tmp_array[@]}"
}

function rstrip() {
  # Usage: rstrip "string" "pattern"
  printf '%s\n' "${1%%$2}"
}

function str_repeat() {
  # Usage: str_repeat "string" "number"
  str=''
  for ((i = 0; i < ${2}; i++)); do
    str=${str}${1}
  done
  echo "${str}"
}

function input_line() {
  # Usage: input_line "string" "length"
  str=''
  for ((i = 0; i < ${#1}; )); do
    if [ -n "${str}" ]; then
      str=${str}"\n"${1:${i}:${2}}
    else
      str=${1:${i}:${2}}
    fi
    i=$((i + ${2}))
  done
  echo -e "${str}"
}

function get_env() {
  # Usage: get_env "key"
  _words=${1-'default'}=
  _grep=$(cat ${service_env_file} | grep ${_words})
  echo ${_grep##${_words}}
}
